package ut.servlet.member;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class ChangePassword extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		// 取得並驗證使用者輸入資訊
		String oldPassword, newPassword;
		if(request.getParameter("old_password") == null || request.getParameter("new_password") == null){
			return;
		}else{
			oldPassword = request.getParameter("old_password");
			if(!oldPassword.matches("[a-zA-Z0-9~!@*]{4,16}")){
				return;
			}
			newPassword = request.getParameter("new_password");
			if(!newPassword.matches("[a-zA-Z0-9~!@*]{4,16}")){
				return;
			}
		}
		// 更新資料庫
		response.setHeader("result", ut.data.User.changePassword(((ut.data.User)request.getSession().getAttribute("user")).id,
			oldPassword, newPassword) + ""); // true: 成功、false: 失敗
	}
}